package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssProductStockHisDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockHis;
import com.aidong.service.BaseJobService;
import com.aidong.util.DateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class AutoCheckStockService extends BaseJobService {
    protected Logger log = Logger.getLogger(AutoCheckStockService.class);
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    PssProductStockHisDao pssProductStockHisDao;
    @Resource
    PssProductStockDao pssProductStockDao;

    public AutoCheckStockService() {
    }

    public synchronized void doJob() {
        Thread th1 = new Thread() {
            public void run() {
                try {
                    try {
                        AutoCheckStockService.this.doAutoCheckStock();
                    } catch (Exception var5) {
                        var5.printStackTrace();
                    }

                } finally {
                    ;
                }
            }
        };
        th1.start();
    }

    private void doAutoCheckStock() throws Exception {
        try {
            List e = this.dicShopCodeDao.getAllShopList();
            Iterator i$ = e.iterator();

            while(i$.hasNext()) {
                DicShopCode shop = (DicShopCode)i$.next();
                ConditionMap cd = new ConditionMap();
                cd.eq("ShopCode", shop.getCode());
                List lstCheck = this.pssProductStockDao.queryList(cd, (new OrderSet()).asc("ProductCode"));
                ArrayList lst = new ArrayList();
                PssProductStockHis pcl = null;
                Iterator e1 = lstCheck.iterator();

                while(e1.hasNext()) {
                    PssProductStock pc = (PssProductStock)e1.next();
                    pcl = new PssProductStockHis();
                    pcl.setShopCode(shop.getCode());
                    pcl.setProductCode(pc.getProductCode());
                    pcl.setQuantity(pc.getQuantity());
                    pcl.setSaveMonth(DateUtil.formatDate(DateUtil.addDays(new Date(), -1), DateUtil.DATE_PATTERN_YYYYMMDD).substring(0, 7));
                    pcl.setCreateUser("Sys");
                    pcl.setCreateTime(new Date());
                    pcl.setUpdateTime(new Date());
                    lst.add(pcl);
                }

                try {
                    this.pssProductStockHisDao.addHisStockList(lst);
                } catch (Exception var10) {
                    this.log.error("自动盘点库存失败::", var10);
                }
            }
        } catch (Exception var11) {
            var11.printStackTrace();
            this.log.error("自动盘点库存失败");
            return;
        }

        this.log.info("自动盘点库存成功！");
    }
}
